package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.common.util.WeatherProvinceUtil;
import com.gable.report.dao.IReportAuthDao;
import com.gable.report.model.ChartsCityWeatherView;
import com.gable.report.model.ChartsOverviewQuery;
import com.gable.report.model.ReportAuth;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalOverviewService;
import com.gable.report.util.HttpProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: 杨青桦
 * @Description:  医院报表总览controller
 * @Date: 2018/1/12 10:58
 */
@Controller
@RequestMapping("charts/hospital/overview")
public class ChartsForHospitalOverviewController {

    @Resource
    HttpServletRequest request;

    @Autowired
    ChartsForHospitalOverviewService chartsForHospitalOverviewService;

    @Autowired
    IReportAuthDao reportAuthDao;

    /**
     * 业务修改：添加警示事项推送需要的参数 修改时间：2018-4-10 修改人:杨青桦
     * @param model
     * @return
     */
    @RequestMapping(value="/page", method= RequestMethod.GET)
    public String page(Model model){
        User user = (User)request.getSession().getAttribute("loginUser");
        if(user == null || user.getHospitalId() == null){
            return "charts-authority";
        }
        model.addAttribute("userId",user.getId());
        model.addAttribute("hospitalId",user.getHospitalId());
        return "charts/hospital/overview";
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询医院所在地区信息，用与前端调用天气插件
     * @Date: 2018/1/17 14:58
     * @return
     */
    @RequestMapping(value="/region", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalRegion(){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            ReportAuth reportAuth = reportAuthDao.queryByHospitalId(user.getHospitalId());
            if(reportAuth.getOperationTag() == 0){
                return null;
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryHospitalRegion(user.getHospitalId()));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


   /**
    * @Author: 杨青桦
    * @Description: 获取医院所在区域天气
    * @Date: 2018/1/23 11:33
    */
    @RequestMapping(value="/weather", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryWeather(){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            Map<String, Object> map = chartsForHospitalOverviewService.queryHospitalRegion(user.getHospitalId());
            String provinceName = (String)map.get("provinceName");
            String ChinaXml = HttpProcessUtil.getXml("china"); //获取全国各省会的天气
            if(StringUtils.isEmpty(provinceName)){ //没有省份信息，直接返回北京市天气
               return new ReturnResultUtil(1,"查询成功",HttpProcessUtil.getCityWeather(ChinaXml,"北京"));
            }else{
                //获取省份的拼音及省份各地级市的天气
                String pinyin = WeatherProvinceUtil.getValue(provinceName);
                String provinceXml = HttpProcessUtil.getXml(pinyin);
                String cityName = (String)map.get("cityName");
                if("市辖区".equals(cityName)){
                    cityName = ((String)map.get("areaName")).replace("区","");
                }else{
                    cityName = cityName.replace("市","");
                }
                //获取区和市的天气，获取不到直接为省会的天气
                ChartsCityWeatherView ccwv = HttpProcessUtil.getCityWeather(provinceXml,cityName);
                if(ccwv == null){
                    ccwv =   HttpProcessUtil.getProvincialCapitalWeather(ChinaXml,WeatherProvinceUtil.getValue(provinceName));
                }
                return new ReturnResultUtil(1,"查询成功",ccwv);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 查询设备数量金额折旧率等基本信息
     * @Date: 2018/1/17 14:58
     * @return
     */
    @RequestMapping(value="/base", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsBase(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryAssetsBase(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  查询重点设备信息
     * @Date: 2018/1/30 11:10
     */
    @RequestMapping(value="/key-point-assets", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryKeyPointAssets(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryKeyPointAssets(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  查询设备分类情况
     * @Date: 2018/1/24 13:09
     * @return
     */
    @RequestMapping(value="/performance", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPerformance(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryPerformance(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备分类平均故障率
     * @Date: 2018/1/24 14:20
     * @return
     */
    @RequestMapping(value="/avg-fault-rate", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAvgFaultRate(){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryAvgFaultRate(user.getHospitalId()));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  警示事项统计
     * @Date: 2018/1/17 14:59
     * @return
     */
    @RequestMapping(value="/warning", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryWarningItems(){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryWarningItems(user.getHospitalId()));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  运维情况(当年)
     * @Date: 2018/1/18 13:30
     * @return
     */
    @RequestMapping(value="/maintenance-year", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMaintenanceConditionsByYear(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryMaintenanceConditionsByYear(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 查询统计详情
     * @Date: 2018/2/1 15:50
     * @param chartsOverviewQuery
     * @return
     */
    @RequestMapping(value="/statistics", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryStatistics(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryStatistics(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备分类负荷率
     * @Date: 2018/1/31 15:00
     */
    @RequestMapping(value="/usage-rate", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryUsageRateForFirstType(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryUsageRateForFirstType(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  运维情况(当月)
     * @Date: 2018/1/18 13:30
     * @return
     */
    @RequestMapping(value="/maintenance-month", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMaintenanceConditionsByMonth(ChartsOverviewQuery chartsOverviewQuery){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryMaintenanceConditionsByMonth(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  警示事项详情
     * @Date: 2018/1/18 17:08
     * @return
     */
    @RequestMapping(value="/warning-list", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryWarningList(@RequestParam Integer tag){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryWarningList(user.getHospitalId(),tag));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  警示事项详情
     * @Date: 2018/1/18 17:08
     * @return
     */
    @RequestMapping(value="/repair-bubble", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryRepairRecordsForBubble(ChartsOverviewQuery chartsOverviewQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            return new ReturnResultUtil(1,"查询成功",chartsForHospitalOverviewService.queryRepairRecords(user.getHospitalId(),chartsOverviewQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  聊天记录
     * @Date: 2018/3/21 12:11
     * @return
     */
    @RequestMapping(value="/repair-chat", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryRepairChatGroups(ChartsOverviewQuery chartsOverviewQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user == null || user.getHospitalId() == null){
                return new ReturnResultUtil(0,"非法账户");
            }
            Map returnMap = chartsForHospitalOverviewService.queryRepairChatGroups(user.getHospitalId(),chartsOverviewQuery);
            returnMap.put("clientId",user.getId());
            returnMap.put("clientName",user.getName());
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }
}
